import React from 'react';
import { Upload, Button } from 'antd';

export default class ImportUpload extends React.Component {
  state = {
    uploading: false,
  };

  handleChange = ({ file, fileList }) => {
    const { onChange } = this.props;

    if (file.status === 'uploading') {
      this.setState({
        uploading: true,
      });
    } else {
      this.setState({
        uploading: false,
      });
    }

    if (onChange) onChange({ file, fileList });
  };

  render() {
    const { children, icon, value, ...uploadProps } = this.props;
    const { uploading } = this.state;
    const im = icon || 'import';
    return (
      <div>
        <Upload
          accept="application/*"
          {...uploadProps}
          showUploadList={false}
          onChange={this.handleChange}
        >
          <Button icon={uploading ? 'loading' : im}>{children || '导入'}</Button>
        </Upload>
      </div>
    );
  }
}
